package com.simple.datax;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

public class LogParser {

	private static final Pattern PATTERN_MSG_ID = Pattern
			.compile("&&&&&&&& (\\d*),");

	// private static final Logger logger =
	// LoggerFactory.getLogger(LogParser.class);

	private BufferedWriter writer;
	private List<String> recvList = new ArrayList<String>(10000);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LogParser parser = new LogParser();
//		String[] files = new String[] {
//				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log",
//				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.1",
//				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.2",
//				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.3",
//				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.4"};
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.5",
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.6",
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.7",
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.8",
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.9",
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.10",
////				"D:\\Java\\workspace\\EC\\datax-transport\\logs\\lns_test.log.11" };
//		// parser.initOutput("parse.log");
		
		File file = new File(".\\logs\\");
		File[] files = file.listFiles();
		
		for (int i = 0; i < files.length; i++) {
			parser.parse(files[i].getName());
			// parser.flush();
		}
		// parser.closeOutput();
		parser.order();
		parser.checkMissing();
	}

	private void order() {
		Collections.sort(recvList);
		// for (int i=0; i<recvList.size(); i++) {
		// System.out.println(recvList.get(i));
		// }
	}

	private void parse(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			Matcher match = null;
			while ((tempString = reader.readLine()) != null) {
				match = PATTERN_MSG_ID.matcher(tempString);
				if (match.find()) {
					// writer.append(match.group(1));
					// writer.append("\n");
					recvList.add(match.group(1));
					// System.out.println(match.group(1));
				}
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	private void initOutput(String file) {
		try {
			writer = new BufferedWriter(new FileWriter(file));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void flush() {
		try {
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void closeOutput() {
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void checkMissing() {
		if (recvList == null || recvList.size() == 0) {
			System.out.println("No data found.");
		}
		String start = recvList.get(0);
		String end = recvList.get(recvList.size() - 1);
		long iStart = Long.valueOf(start);
		long iEnd = Long.valueOf(end);
		List<String> full = new ArrayList<String>();
		for (long i = iStart; i <= iEnd; i++) {
			full.add(String.format("%1$09d", i));
		}

		full.removeAll(recvList);
		StringBuffer miss = new StringBuffer("");
		System.out.println("Missing count:" + full.size());
		miss.append("Missing count:" + full.size()).append("\n");
		for (String str : full) {
			System.out.println(str);
			miss.append(str).append("\n");
		}
		
		writeToFile("./logs/missing.txt", miss);

	}

	private void writeToFile(String fileName, StringBuffer full) {
		try {
			File writefile = new File(fileName);

			FileUtils.writeByteArrayToFile(writefile, full.toString()
					.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
